<?php

require_once "managerSession.php";
error_reporting(0);
if(isset($_POST["UpdateWithdraw"])){
    $id=$_REQUEST["UpdateWithdraw"];
    $withdraw=$core->GetUserWithdrawById($id);
    $userId=$withdraw["user_id"];
    $user=$core->GetUserById($userId);
    if($user["remain_bonus"]>=$withdraw["money"]){
        echo $user["phone"]."将提现".$withdraw["money"];

    }else{
        echo "余额不足";
    }
    exit;
}
if(isset($_POST["managerEdit"])){
    $manager1=$core->GetManageById($_REQUEST["managerEdit"]);
    if(is_array($manager1)&&!empty($manager1)){
        $manager1["oRole"]=$manager['role'];
        $manager1["oId"]=$manager['id'];
        $result=json_encode($manager1);
        echo $result;
        exit;
    }
    exit;
}

if (isset($_POST["expendAddPrize1"])&&isset($_POST["expendAddPrize1"])){
    echo empty($core->UpdateExpend_Prize($_POST["expendAddPrize1"],$_POST["expendAddPrize2"]))?"兑换成功":"兑换失败";
    exit;
}
if(isset($_POST["tag"])&&isset($_POST["details"])){
    $result=$core->SetArticle($_POST["details"],$_POST["tag"]);
    echo empty($result)?"修改成功":$result;
    exit;
}
if(isset($_POST["tag"])&&!isset($_POST["details"])){
    if(!empty($content=$core->GetTagArticle($_POST["tag"])["details"])){
        echo $content;
    }else{
        echo "没有内容";
    }
    exit;
}

if (isset($_POST["cz"])&&($_POST["cz"])=="delManager"&&$manager["role"]=="超级管理员"){
    echo $core->DelManage($_POST["manager"] ,$_POST["del"]);
    exit;
}elseif(isset($_POST["manager"])&&isset($_POST["del"])){
    $manager2=$core->GetManageById($_POST["del"]);
    echo ($manager["role"]=="超级管理员"&&$manager2["role"]!="超级管理员")?json_encode($manager2):"";
    exit;
}

if(isset($_POST["getPrizeInfo"])){
    $id=$_POST["getPrizeInfo"];
    $prize=$core->GetPrizeById($id);
    if(empty($prize)){
        echo "获取失败";
    }else{
        echo json_encode($prize);
    }
    exit;
}

if(isset($_POST["chge"])&&isset($_POST["name"])&&isset($_POST["expend"])&&isset($_POST["remark"])&&isset($_POST["status"])&&$manager["role"]=="超级管理员"){
    $id=$_POST["chge"];
    $name=$_POST["name"];
    $expend=$_POST["expend"];
    $remark=$_POST["remark"];
    $status=$_POST["status"];
    if(empty($id)){
        if($core->AddPrize($name,$expend,$remark,$status)){
            echo "添加失败";
        }else{
            echo "添加成功";
        }

    }elseif(empty($prize=$core->GetPrizeById($id))){
        if($core->AddPrize($name,$expend,$remark,$status)){
            echo "添加失败";
        }else{
            echo "添加成功";
        }

    }elseif(!empty($prize=$core->GetPrizeById($id))){
        if($core->UpdatePrize($id,$name,$expend,$remark,$status)){
            echo "修改失败";
        }else{
            echo "修改成功";
        }

    }
    exit;
}

if(isset($_POST["Mid"])&&isset($_POST["name"])&&isset($_REQUEST["role"])&&($manager["role"]=="超级管理员"||$manager["id"]==$_POST["Mid"])){
    $message=$core->SetManage($manager["id"], $_POST["Mid"],$_POST["name"],"",$_POST["role"] );
    if(empty($message)){
        $message="修改成功";
    }
    echo $message;
    exit;
}

if (isset($_POST["expend"])){
    $expend=$core->GetUserExpendAndPrize($_POST["expend"]);
    echo json_encode($expend);
    exit;
}
if (isset($_POST["bonus"])){
    $expend=$core->GetUserBonusAndFromUser($_POST["bonus"]);
    echo json_encode($expend);
    exit;
}
if(isset($_POST["getPrize"])){
    $expend=$core->getUserExpendById($_POST["getPrize"]);
    $prizes=$core->GetPrizes();
    for ($i=0;$i<count($prizes);++$i){
        $prizes[$i]["user_id"]=$expend['user_id'];
        if($prizes[$i]["expend"]<=$expend["money"]){
            $prizes[$i]["allow"]=true;
        }else{
            $prizes[$i]["allow"]=false;
        }
    }
    echo json_encode($prizes);
    exit;
}


if(isset($_REQUEST["ratio1"])&&isset($_REQUEST["ratio2"])&&isset($_REQUEST["ratio3"])&&$manager["role"]=="超级管理员"){
    $core->SetUserRatio("一级",$_REQUEST["ratio1"]);
    $core->SetUserRatio("二级",$_REQUEST["ratio2"]);
    $core->SetUserRatio("三级",$_REQUEST["ratio3"]);
    exit;
}